package com.example.a14110100409.yuejiandemo.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.a14110100409.yuejiandemo.R;
import com.example.a14110100409.yuejiandemo.fragment.ClubFragment;
import com.example.a14110100409.yuejiandemo.fragment.HomeFragment;
import com.example.a14110100409.yuejiandemo.fragment.MyFragment;
import com.example.a14110100409.yuejiandemo.fragment.ShaituFragment;
import com.example.a14110100409.yuejiandemo.fragment.TrainFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
    private LinearLayout mTabTrain;
    private LinearLayout mTabClub;
    private LinearLayout mTabHome;
    private LinearLayout mTabPhoto;
    private LinearLayout mTabMy;

    private ImageButton mImgTrain;
    private ImageButton mImgClub;
    private ImageButton mImgHome;
    private ImageButton mImgPhoto;
    private ImageButton mImgMy;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        // this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);
        LinearLayoutManager LL = new LinearLayoutManager(this);

        initView();//初始化
        initEvent();//初始化事件
        setSelect(0);//
    }

    private void initEvent()
    {
        mTabTrain.setOnClickListener(this);
        mTabClub.setOnClickListener(this);
        mTabPhoto.setOnClickListener(this);
        mTabMy.setOnClickListener(this);
        mTabHome.setOnClickListener(this);
    }

    private void initView()
    {
        mTabTrain = (LinearLayout) findViewById(R.id.id_tab_peixun);
        mTabClub = (LinearLayout) findViewById(R.id.id_tab_club);
        mTabPhoto = (LinearLayout) findViewById(R.id.id_tab_shaitu);
        mTabMy = (LinearLayout) findViewById(R.id.id_tab_wo);
        mTabHome = (LinearLayout) findViewById(R.id.id_tab_shouye);

        mImgTrain = (ImageButton) findViewById(R.id.id_tab_peixun_img);
        mImgClub = (ImageButton) findViewById(R.id.id_tab_club_img);
        mImgPhoto = (ImageButton) findViewById(R.id.id_tab_shaitu_img);
        mImgMy = (ImageButton) findViewById(R.id.id_tab_wo_img);
        mImgHome = (ImageButton) findViewById(R.id.id_tab_shouye_img);
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new TrainFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                mImgTrain.setImageResource(R.drawable.ic_tab_peixun_pressed);
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new ClubFragment()
                    ;transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);

                }
                mImgClub.setImageResource(R.drawable.ic_tab_club_pressed);
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new ShaituFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);
                }
                mImgPhoto.setImageResource(R.drawable.ic_tab_shaitu_pressed);
                break;
            case 3:
            if (mTab04 == null)
            {
                mTab04 = new MyFragment();
                transaction.add(R.id.id_content, mTab04);
            } else
            {
                transaction.show(mTab04);
            }
            mImgMy.setImageResource(R.drawable.ic_tab_wo_pressed);
            break;

            case 4:
                if (mTab05 == null)
                {
                    mTab05 = new HomeFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else
                {
                    transaction.show(mTab05);
                }
                mImgHome.setImageResource(R.drawable.ic_tab_shouye_pressed);
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_tab_peixun:
                setSelect(0);
                break;
            case R.id.id_tab_club:
                setSelect(1);
                break;
            case R.id.id_tab_shaitu:
                setSelect(2);
                break;
            case R.id.id_tab_wo:
                setSelect(3);
                break;
            case R.id.id_tab_shouye:
                setSelect(4);
                break;

            default:
                break;
        }
    }


    private void resetImgs()
    {
        mImgTrain.setImageResource(R.drawable.ic_tab_peixun_normal);
        mImgClub.setImageResource(R.drawable.ic_tab_club_normal);
        mImgPhoto.setImageResource(R.drawable.ic_tab_shaitu_normal);
        mImgMy.setImageResource(R.drawable.ic_tab_wo_normal);
        mImgHome.setImageResource(R.drawable.ic_tab_shouye_normal);
    }
}
